<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <ArticleItem
          v-for="item in listSearch"
          :key="item.art_id"
          :title="item.title"
          :name="item.aut_name"
          :comm="item.comm_count"
          :timer="item.pubdate"
          :id="item.art_id"
          :cover="item.cover"
          :isSHwo="false"
          @click.native="detailGO(item.art_id)"
        ></ArticleItem>
      </van-list>
    </div>
  </div>
</template>

<script>
import { searchResultsAPI } from '@/api/index.js'
import ArticleItem from '@/components/ArticleItem.vue'
export default {
  name: 'SearchResult',
  props: ['kw'],
  data() {
    return {
      page: 1,
      listSearch: [],
      loading: false,
      finished: false
    }
  },
  methods: {
    detailGO(e) {
      this.$router.push({
        path:`/detail?art_id=${e}`
      })
    },
    async onLoad() {
      if (this.listSearch.length > 0) {
        this.page++
        const { data: ress } = await searchResultsAPI({
          page: this.page,
          per_page: 10,
          q: this.kw
        })
        if (ress.data.results.length === 0) {
          this.finished = true

          return
        } else {

          this.listSearch = [...this.listSearch, ...ress.data.results]
          this.loading = false
        }
      }
    }
  },
  async created() {
    const { data: res } = await searchResultsAPI({
      page: this.page,
      per_page: 10,
      q: this.kw
    })

    this.listSearch = res.data.results
  },
  components: { ArticleItem }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
